package com.shure.encrypt.utils;

import cn.hutool.core.text.UnicodeUtil;
import org.apache.commons.lang3.StringUtils;

public class EncryptUtils {

    private static final int n = 6;

    public static void main(String[] args) {
        String s = encrypt("");
        System.out.println(s);
        String a = decrypt(s);
        System.out.println(a);
    }

    public static String encrypt(String data) {
        if (StringUtils.isBlank(data)) {
            return "";
        }
        String ss = "0123456789abcdef";
        String en = UnicodeUtil.toUnicode(data, false);
        String[] arr = en.split("\\\\u");
        String[] res = new String[arr.length - 1];
        for (int i = 0; i < arr.length; i++) {
            if (i == 0)
                continue;
            String s = arr[i];
            String r = "";
            for (int j = 0; j < s.length(); j++) {
                char a = s.charAt(j);
                int inx = ss.indexOf(a) + n;
                if (inx >= 16) {
                    inx = inx - 16;
                }
                char b = ss.charAt(inx);
                r = r + b;
            }
            res[i - 1] = r;
        }
        String rr = "\\u" + String.join("\\u", res);
        return UnicodeUtil.toString(rr);
    }

    public static String decrypt(String data) {
        if (StringUtils.isBlank(data)) {
            return "";
        }
        String ss = "0123456789abcdef";
        String en = UnicodeUtil.toUnicode(data, false);
        String[] arr = en.split("\\\\u");
        String[] res = new String[arr.length - 1];
        for (int i = 0; i < arr.length; i++) {
            if (i == 0)
                continue;
            String s = arr[i];
            String r = "";
            for (int j = 0; j < s.length(); j++) {
                char a = s.charAt(j);
                int inx = ss.indexOf(a) - n;
                if (inx < 0) {
                    inx = inx + 16;
                }
                char b = ss.charAt(inx);
                r = r + b;
            }
            res[i - 1] = r;
        }
        String rr = "\\u" + String.join("\\u", res);
        return UnicodeUtil.toString(rr);
    }

}
